package fm.dongman.animefm.view.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.BounceInterpolator;

import fm.dongman.animefm.R;

/**
 * Created by alex on 2017/10/17.
 *
 * @description 用于显示评分的圆形rating条，评分值为1-5的float值
 */

public class CircleRatingDisplayView extends View {

    private Paint mPaint;
    private float mRating;      //评分，值只能是1-5
    private int strokeWidth = 2;    //描边宽度
    private int mRatingTotal = 5;     //满分的总数
    private int mRatingColor;
    private int degree = 360;

    public CircleRatingDisplayView(Context context) {
        this(context, null);
    }

    public CircleRatingDisplayView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        //获取xml自定义属性的值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleRatingDisplayView);
        //xml设置的rating值
        mRating = typedArray.getFloat(R.styleable.CircleRatingDisplayView_rating, 0);
        typedArray.recycle();

        //初始化画笔
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRatingColor = ContextCompat.getColor(getContext(), R.color.primary); //使用程序主题色
        mPaint.setColor(0xffcccccc);

        //设置描边的宽度
        strokeWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getContext().getResources().getDisplayMetrics());
        mPaint.setStrokeWidth(strokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        int radius = getWidth() < getHeight() ? getWidth() / 2 - 20 : getHeight() / 2 - 20;
        canvas.drawCircle(centerX, centerY, radius, mPaint);

        mPaint.setColor(mRatingColor);

        //评分不低于0，则绘制评分结果
        if (mRating > 0) {
            float left = centerX - radius;
            float top = centerY - radius;
            float right = centerX + radius;
            float bottom = centerY + radius;

            float endAngle = degree * (mRating / mRatingTotal);
            canvas.drawArc(left, top, right, bottom, 0, endAngle, false, mPaint);
        }
        mPaint.setColor(0xffcccccc);
    }

    public void setRatingColorResoure(@ColorRes int colorRes) {
        mRatingColor = ContextCompat.getColor(getContext(), colorRes);
        invalidate();
    }

    public void setRatingColor(int color) {
        mRatingColor = color;
        invalidate();
    }

    public float getRating() {
        return mRating;
    }

    public void setRating(float rating) {
        if (rating > 5)
            throw new IllegalArgumentException(this.toString() + "评分数值不能超过5！");

        mRating = rating;
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            doAnim();
        }
        return super.onTouchEvent(event);
    }

    private void doAnim() {
        ValueAnimator animator = ValueAnimator.ofInt(0, 360);
        animator.setInterpolator(new BounceInterpolator());
        animator.setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                degree = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();

    }
}
